package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.duer.smartmate.out.oauth.IResponseCallback;
import com.baidu.duer.smartmate.protocol.dlp.bean.deviceinfo.DeviceInfoPayLoad;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;
import com.baidu.duer.smartmate.proxy.controller.DCSDataObserver;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.util.DemoUtils;

/**
 * Created by niufei01 on 2017/9/11.
 * <p>
 * 设备基本信息页
 */

public class DeviceInfoDemo extends BaseFragment implements DCSDataObserver<DeviceInfoPayLoad> {

    ControllerManager controllerManager;
    ViewHolder viewHolder;

    public final static String TAG = "DeviceInfoDemo";

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.device_info, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        viewHolder = new ViewHolder(view);
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerDeviceInfoObserver(this);
        viewHolder.getstatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.getDeviceInfoStatus();
            }
        });
        viewHolder.getname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final String name = DemoApp.getInstance().getCurreDuerDevice().syncGetDeviceName(getMActivity());
                        getMActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                viewHolder.edittext.setText("音箱名称：" + name);
                            }
                        });
                    }
                }).start();
            }
        });
        viewHolder.setname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoApp.getInstance().getCurreDuerDevice().asyncSetDeviceName(getMActivity(), "小度之家测试", new IResponseCallback() {
                    @Override
                    public void onSuccess() {
                        viewHolder.edittext.setText("设置名字成功！！！！");
                    }

                    @Override
                    public void onError(long status, String msg) {
                        viewHolder.edittext.setText("设置名字失败！！！！");
                    }
                });
            }
        });
        viewHolder.unbindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DemoApp.getInstance().getCurreDuerDevice().unoauth(new IResponseCallback() {
                    @Override
                    public void onSuccess() {
                        viewHolder.edittext.setText("解绑成功");
                    }

                    @Override
                    public void onError(long code, String message) {
                        viewHolder.edittext.setText("解绑失败");
                    }
                });
            }
        });
    }

    @Override
    public void onDataChanaged(String event, DeviceInfoPayLoad message) {
        viewHolder.edittext.setText(message != null ? DemoUtils.toFormatJson(message) : "数据为空");
    }

    public class ViewHolder {
        public View rootView;
        public EditText edittext;
        public Button getstatus;
        public Button unbindBtn;
        public Button getname;
        public Button setname;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.edittext = (EditText) rootView.findViewById(R.id.edittext);
            this.getstatus = (Button) rootView.findViewById(R.id.getstatus);
            this.unbindBtn = (Button) rootView.findViewById(R.id.unbind_button);
            this.getname = (Button) rootView.findViewById(R.id.getname);
            this.setname = (Button) rootView.findViewById(R.id.setname);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (controllerManager != null) {
            controllerManager.unregisterDeviceInfoObserver(this);
        }
    }
}
